// --- START OF FILE PlacementSurface.cs ---

using System.Collections.Generic;
using UnityEngine;

namespace PlaceSystem
{
    public class PlacementSurface : MonoBehaviour
    {
        [Header("用于计算表面范围的碰撞体")]
        public List<Collider> colliders;

        [Header("当前碰撞层")]
        public int layer;

        private void OnEnable()
        {
            PlaceSystemController.EnablePlacementSurface(this);
        }

        private void OnDisable()
        {
            PlaceSystemController.DisablePlacementSurface(this);
        }

        /// <summary>
        /// 启用碰撞器
        /// </summary>
        public void EnableColliders()
        {
            if (colliders == null || colliders.Count == 0) return;
            foreach (BoxCollider collider in colliders)
            {
                collider.gameObject.layer = layer;
            }
        }

        /// <summary>
        /// 禁用碰撞器
        /// </summary>
        public void DisableColliders()
        {
            if (colliders == null || colliders.Count == 0) return;
            foreach (BoxCollider collider in colliders)
            {
                collider.gameObject.layer = 2;
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="map"></param>
        public void Register(Dictionary<Collider, PlacementSurface> map)
        {
            if (colliders == null || colliders.Count == 0) return;
            EnableColliders();
            foreach (BoxCollider collider in colliders)
            {
                map.TryAdd(collider, this);
            }
        }

        /// <summary>
        /// 移除注册
        /// </summary>
        /// <param name="map"></param>
        public void Unregister(Dictionary<Collider, PlacementSurface> map)
        {
            if (colliders == null || colliders.Count == 0) return;
            DisableColliders();
            foreach (BoxCollider collider in colliders)
            {
                map.Remove(collider);
            }
        }
    }
}

// --- END OF FILE PlacementSurface.cs ---